<template>
  <div>
    <div class="edit">
        <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="课题编号">
                <el-input v-model="form.subjectId"></el-input>
            </el-form-item>
             <el-form-item label="课题名称">
                <el-input v-model="form.subjectName"></el-input>
            </el-form-item>
             <el-form-item label="预选人数">
                <el-input v-model="form.primaryNum"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="saveSubject('form')">更改</el-button>
                <el-button @click="$router.back(-1)">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
  </div>
    
</template>
<script>
import  api from '@/api/subject/subjectApi'
  export default {
    data() {
      return {
        form: {
          subjectId: '',
          subjectName: '',
          primaryNum: ''
        }
      }
    },
    created() {
        //获取路由id
        const Id = this.$route.params.subjectId
        
        this.getSubject(Id)
    },
    methods: {
      getSubject(id){
          api.getsubjectInfo(id)
          .then(response =>{
            console.log(response)
             this.form.subjectId =response.data.subjectId
             this.form.subjectName=response.data.subjectName
             this.form.primaryNum= response.data.primaryNum

          })
      },
      saveSubject(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            console.log("****")
          
              api.saveSubjectInfo(this.form)
              .then(response=>{
                this.$alert('保存成功', '成功', {
                  dangerouslyUseHTMLString: true
                });
              }).catch(error =>{
                this.$alert('<strong>添加失败</strong>', '失败', {
                  dangerouslyUseHTMLString: true
                });
              })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      
    }
  }
</script>
<style>
.edit{
  width: 500px;
  margin: 100px auto;

}
</style>